package com.csi.controller.back;

import com.csi.domain.Employee;
import com.csi.domain.vo.EmployeeDetail;
import com.csi.service.LoginService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/back/login")
@Tag(name = "backLogin",description = "后台登录接口")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 根据类型查找名字
     * @param platForm
     * @return
     */
    @GetMapping("/findName")
    public Object findName(@RequestParam("type")String platForm){
        List<Object> platFormNames = loginService.findName(platForm);
        if (platFormNames != null){
            return new JsonResult<>(platFormNames);
        }

        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
    }

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Object login(@RequestBody Employee employee){
        Employee employee1 = loginService.login(employee);
        if (employee1 != null){
            employee1.setType(employee.getType());
            String token = JwtUtil.getToken(employee1.getId(),employee1.getRoleId(),employee1.getHotelId(),employee1.getTravelId(),employee1.getType());
            return new JsonResult<>(token);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"登陆失败，用户名或密码错误！");
    }

    /**
     * 根据用户名和酒店或者旅行社id查找用户相信信息，用于存储到前端store
     * @param username
     * @param type
     * @param typeNameId
     * @return
     */
    @GetMapping("/findByName")
    public Object findByName(@RequestParam("username") String username,@RequestParam("type") String type,@RequestParam("typeNameId") long typeNameId){
        System.out.println("username = " + username + ", type = " + type + ", typeNameId = " + typeNameId);
        EmployeeDetail employee = loginService.findByName(username,type,typeNameId);
        employee.setType(type);
        if (employee != null){
            System.out.println(employee);
            return new JsonResult<>(employee);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查找失败");
    }
}
